<template>
  <div>
    <!-- 添加弹出框 -->
    <el-dialog
      :title="dialogFormTitle"
      :visible.sync="dialogFormVisible"
      @close="closedialog"
    >
      <el-form
        ref="produceForm"
        :rules="rules"
        :model="productionreceivemodify"
        label-width="80px"
        style="width: 300px"
      >
        <el-form-item label="原料名称" prop="mname">
          <label v-if="isEdit">{{ productionreceivemodify.mname }}</label>
          <el-select
            v-model="productionreceivemodify.mname"
            placeholder="请选择产品"
            clearable
            v-if="!isEdit"
          >
            <el-option
              v-for="item in raws"
              :key="item.value"
              :label="item.rname"
              :value="item.rname"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="申请数量" prop="mnum">
          <label v-if="isEdit">{{ productionreceivemodify.mnum }}</label>
          <el-input
            v-model="productionreceivemodify.mnum"
            clearable
            v-if="!isEdit"
          ></el-input>
        </el-form-item>

        <el-form-item label="原料单位" prop="munit">
          <label v-if="isEdit">{{ productionreceivemodify.munit }}</label>
          <el-select
            v-model="productionreceivemodify.munit"
            placeholder="请选择单位"
            v-if="!isEdit"
          >
            <el-option label="千克" value="千克"></el-option>
            <el-option label="吨" value="吨"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="申请时间" prop="mtime">
          {{ productionreceivemodify.mtime }}
        </el-form-item>

        <el-form-item label="领料员工" prop="mperson">
          <label v-if="isEdit">{{ productionreceivemodify.mperson }}</label>
          <el-select
            v-model="productionreceivemodify.mperson"
            filterable
            placeholder="请选择领料员工"
            clearable
            v-if="!isEdit"
          >
            <el-option
              v-for="item in employees"
              :key="item.value"
              :label="item.ename"
              :value="item.ename"
            >
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="领取状态" prop="mstate" v-if="isshow">
          <label v-if="isEdit">{{ productionreceivemodify.mstate }}</label>
          <el-select
            v-model="productionreceivemodify.mstate"
            placeholder="请选择状态"
            v-if="!isEdit"
          >
            <el-option label="未领取" value="未领取"></el-option>
            <el-option label="已领取" value="已领取"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm">取 消</el-button>
        <el-button type="primary" @click="handEditAdd">确定</el-button>
      </div>
    </el-dialog>

    <!-- 搜索表单 -->
    <el-form
      ref="productionreceive"
      :model="productionreceive"
      label-width="80px"
      :inline="true"
    >
      <el-form-item>
        <el-button type="primary" @click="batchDelete">批量删除</el-button>
      </el-form-item>

      <el-form-item label="领料单号">
        <el-input v-model="productionreceive.pmid" clearable></el-input>
      </el-form-item>

      <el-form-item label="原料名称">
        <el-input v-model="productionreceive.mname" clearable></el-input>
      </el-form-item>

      <el-form-item label="领取状态" style="width: 120px">
        <el-select v-model="productionreceive.mstate" placeholder="请选择状态">
          <el-option label="已领取" value="已领取"></el-option>
          <el-option label="未领取" value="未领取"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="申请状态" style="width: 120px">
        <el-select v-model="productionreceive.astate" placeholder="请选择状态">
          <el-option label="待审批" value="待审批"></el-option>
          <el-option label="已通过" value="已通过"></el-option>
          <el-option label="未通过" value="未通过"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="领料日期">
        <el-input type="date" v-model="productionreceive.mtime"></el-input>
      </el-form-item>

      <el-form-item label="领料员工">
        <el-input v-model="productionreceive.mperson" clearable></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询所有信息</el-button>
        <el-button @click="handAdd">申请领料</el-button>
        <el-button @click="clearAllFields">清空</el-button>
      </el-form-item>
    </el-form>

    <!-- 展示数据 -->
    <el-table
      :data="pageBean.beanList"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="pmid" label="领料单号" width="140" align="center">
      </el-table-column>
      <el-table-column prop="mname" label="原料名称" width="100" align="center">
      </el-table-column>
      <el-table-column
        prop="myield"
        label="原料数量"
        width="100"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="mtime" label="领料日期" width="140" align="center">
      </el-table-column>
      <el-table-column
        prop="mperson"
        label="领料员工"
        width="140"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="mstate"
        label="领取状态"
        width="140"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.mstate === '已领取'
                ? 'success'
                : scope.row.mstate === '未领取'
                ? 'danger'
                : 'primary'
            "
            disable-transitions
            >{{ scope.row.mstate }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="astate"
        label="审批状态"
        width="140"
        align="center"
      >
        <template slot-scope="scope">
          <el-tag
            :type="
              scope.row.astate === '已通过'
                ? 'success'
                : scope.row.astate === '未通过'
                ? 'danger'
                : 'primary'
            "
            disable-transitions
            >{{ scope.row.astate }}</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 展示页码 -->
    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[1, 3, 6]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageBean.totalRecord"
      >
      </el-pagination>
    </div>
  </div>
</template>
  
  <script>
import axios from "axios";

export default {
  data() {
    var checkenum = (rule, value, callback) => {
      if (value === undefined || value === "") {
        callback(new Error("填写数量"));
      } else if (!/^[1-9]\d*$/.test(value)) {
        callback(new Error("数量格式错误"));
      }else {
        callback();
      }
    };
    return {
      isEdit:false,
      isshow:false,
      produceForm: null,
      dialogFormVisible: false,
      multipleSelection: [],
      dialogFormTitle: "",
      pageIndex: 1,
      pageSize: 6,
      pageBean: {},
      raws: {},
      employees: "",
      rules: {
        mname: [{ required: true, message: "请输入产品名称", trigger: "blur" }],
        munit: [
          { required: true, message: "请选择产品单位", trigger: "change" },
        ],
        mperson: [{ required: true, message: "请输入名字", trigger: "blur" }],
        mnum: [
          { required: true, message: "请输入产品数量", trigger: "blur" },
          { validator: checkenum, trigger: "blur" },
        ],
        mtime: [
          {
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        mstate: [
          {
            required: true,
            message: "请选择状态",
            trigger: "change",
          },
        ],
      },
      productionreceive: {},
      productionreceivemodify: {},
    };
  },
  mounted() {
    this.getAll();
    this.getAllRaws();
    this.getAllEmployee();
  },
  methods: {
    //  获取当前时间
    currentDate() {
      return (
        new Date().getFullYear() +
        "-" +
        ("0" + (new Date().getMonth() + 1)).slice(-2) +
        "-" +
        ("0" + new Date().getDate()).slice(-2)
      );
    },
    // 查询所有信息
    getAll() {
      axios
        .post(
          "/produced/findAllReceive/" + this.pageIndex + "/" + this.pageSize,
          this.productionreceive
        )
        .then((resp) => {
          if (resp.data.code == 200) {
            this.pageBean = resp.data.data;
          }
        });
    },
    // 查询所有原料
    getAllRaws() {
      axios.post("/produced/findAllRaws").then((resp) => {
        this.raws = resp.data.data;
      });
    },
    // 查询所有生产员工
    getAllEmployee() {
      axios.post("/produced/findAllEmployee").then((resp) => {
        this.employees = resp.data.data;
      });
    },
    //  查询按钮
    onSubmit() {
      this.pageIndex = 1;
      this.pageSize = 6;
      this.getAll();
    },
    //  一键清空
    clearAllFields() {
      this.productionreceive = {
        poid: "",
        pname: "",
        stime: "",
      };
      this.getAll();
    },
    //  添加弹出框
    handAdd() {
      this.dialogFormVisible = true,
      this.dialogFormTitle = "申请领料",
      this.productionreceivemodify.mtime=this.currentDate();
    },
    //  申请、修改领料单
    handEditAdd() {
      this.$refs.produceForm.validate((valid) => {
        if (valid) {
          if (this.dialogFormTitle == "申请领料") {
            axios
              .post("/produced/addReceive", this.productionreceivemodify)
              .then((resp) => {
                if (resp.data.code == 200) {
                  this.$message("创建成功");
                  this.getAll();
                } else {
                  this.$message.error(JSON.stringify(resp.data.msg));
                }
                this.resetForm();
              });
          } else {
            if(this.productionreceivemodify.mstate === "已领取" && this.isEdit === false){
              axios
          .post("/produced/modifyReceive", this.productionreceivemodify)
          .then((resp) => {
            if (resp.data.code == 200) {
              this.$message("领取成功");
              this.getAll();
            } else {
              this.$message.error(JSON.stringify(resp.data.msg));
            }
          });
            }
        this.resetForm();
      }
      }else {
            this.$message("验证失败");
          }
        });
    },
    //  编辑领料单
    handEdit(index, row) {
      this.dialogFormVisible = true,
      this.dialogFormTitle = "修改领料单",
      this.productionreceivemodify = { ...row };
      this.isshow = true;
      this.isEdit = this.productionreceivemodify.mstate === "已领取"
    },
    //  清空表单
    resetForm() {
      this.isEdit=false;
      this.$refs.produceForm.resetFields();
      this.dialogFormVisible = false;
    },
    //  关闭弹出框
    closedialog() {
      //  情况数据
      this.resetForm();
      this.isshow = false;
    },
    //  复选框选中
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 分页
    handleSizeChange(val) {
      //  重新查询
      this.pageSize = val;
      this.getAll();
    },
    handleCurrentChange(val) {
      //  重新查询
      this.pageIndex = val;
      this.getAll();
    },
    //  删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          axios.post("/produced/deleteM", row).then((resp) => {
            if (resp.data.code == 200) {
              this.$message("删除成功");
              this.getAll();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //  批量删除
    batchDelete(index, row) {
      if (this.multipleSelection.length > 0) {
        let myids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          myids.push(this.multipleSelection[i].id);
        }
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            axios
              .post("/produced/deletebatchgetM", { ids: myids })
              .then((resp) => {
                if (resp.data.code == 200) {
                  this.$message("删除成功");
                  this.getAll();
                } else {
                  this.$message.error("删除失败");
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      } else {
        this.$message.error("请选择要删除的数据");
      }
    },
  },
};
</script>
  
  <style>
</style>